package co.fitstart.weapp.api.web.controller.course;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.courseware.fitnessgroup.BatchCourseware;
import co.fitstart.entity.courseware.fitnessgroup.DailyCourseware;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.weapp.api.service.CourseRoundService;
import co.fitstart.weapp.api.service.CoursewareService;
import co.fitstart.weapp.api.service.UserService;

/**
 * 
 * @author haiyan_xiao
 *
 */
@RestController
@RequestMapping("/year-rounds/{roundId}")
public class YearCoursewareController {

    private static final Logger logger = LoggerFactory.getLogger(YearCoursewareController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private CourseRoundService courseRoundService;

    @Autowired
    private CoursewareService coursewareService;

    /**
     * 
     * @param unionid
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/course-coursewares", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult listCourseCourseware(@RequestParam("unionid") String unionid,
                                            @PathVariable("roundId") Long courseRoundId) {
        HttpResult httpResult = new HttpResult();
        
        try {
            User user = userService.load(unionid);
            Student student = userService.getStudent(user);
            CourseRound courseRound = courseRoundService.load(courseRoundId);
            
            courseRoundService.validate(student, courseRound);
            
            BatchCourseware batchCourseware = coursewareService.loadBatchCourseware(courseRound);
            
            httpResult.success().message("获取数据成功")
                                .data("dailys", batchCourseware.getDailyCoursewares());
        } catch (BusinessException e) {
            httpResult.fail("1").message(e.getMessage());
        } catch (Exception e) {
            logger.error("listYearCourseCourseware() error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }

    @RequestMapping(value = "/course-coursewares/{dailyId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult getCourseCourseware(@RequestParam("unionid") String unionid,
                                           @PathVariable("roundId") Long courseRoundId,
                                           @PathVariable("dailyId") Long dailyId) {
        HttpResult httpResult = new HttpResult();
        
        try {
            User user = userService.load(unionid);
            Student student = userService.getStudent(user);
            DailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(dailyId);
            
            if(student == null || dailyCourseware == null || dailyCourseware.getBatch().getCourseRound().getStudent() != student) {
                httpResult.fail("1").message("课程异常");
            } else {
                httpResult.success().message("获取数据成功")
                                    .data("tags", coursewareService.listCoursewareTags(dailyCourseware.getBatch().getCourseRound().getStudent().getArrayTagIds()))
                                    .data("coursewares", coursewareService.listCourseware(dailyCourseware));
            }
        } catch (Exception e) {
            logger.error("getCourseCourseware() error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }
}
